package services

import (
	"context"
	"encoding/json"
	"fmt"

	"gitee.com/wangshh_123/cross-pay-go/src/platform/providers/juswaypay"
	"gitee.com/wangshh_123/cross-pay-go/src/platform/providers/juswaypay/models"
)

// GetUserInfo 获取用户信息
func GetUserInfo(ctx context.Context, client *juswaypay.JuswayPayClient) (*models.UserInfoResponse, error) {
	path := "/v1/user/info"

	resp, err := client.Get(ctx, path)
	if err != nil {
		return nil, fmt.Errorf("get user info failed: %v", err)
	}

	var result models.UserInfoResponse
	if err := json.Unmarshal(resp, &result); err != nil {
		return nil, fmt.Errorf("unmarshal response failed: %v", err)
	}

	if !result.Ret {
		return nil, fmt.Errorf("get user info failed: status=%d", result.Status)
	}

	return &result, nil
}

// GetMemberCenter 获取会员中心信息
func GetMemberCenter(ctx context.Context, client *juswaypay.JuswayPayClient) (*models.MemberCenterResponse, error) {
	path := "/v1/member/center"

	resp, err := client.Get(ctx, path)
	if err != nil {
		return nil, fmt.Errorf("get member center failed: %v", err)
	}

	var result models.MemberCenterResponse
	if err := json.Unmarshal(resp, &result); err != nil {
		return nil, fmt.Errorf("unmarshal response failed: %v", err)
	}

	if !result.Ret {
		return nil, fmt.Errorf("get member center failed: status=%d", result.Status)
	}

	return &result, nil
}
